import router from './index'
import store from './../store'
import {getUrlS, jssdk} from '@/api/login'
import qs from 'qs'
import Cookies from 'js-cookie'
// import axios from 'axios'
import { login } from '@/utils/login'
window.entryUrl = window.location.href.split('#')[0]
// console.log(window.entryUrl)
router.beforeEach((to, from, next) => {
  store.commit('updateLoadingStatus', { isLoading: true })
  let theOther = ''
  if (to.query.shareid) {
    Cookies.set('theRequest', to.query)
  }
  if (Cookies.get('theRequest')) {
    theOther = JSON.parse(Cookies.get('theRequest'))
  }
  // console.log(Cookies.get('theRequest'))
  if (to.name === 'home' && to.query && to.query.code && to.query.appid) {
    let params = {
      appid: to.query.appid,
      state: to.query.state,
      code: to.query.code,
      pid: theOther !== '' && theOther.shareid ? theOther.shareid : 0
      // theOther.shareid !== undefined && theOther.shareid !== '' ? theOther.shareid : 0
    }
    if (theOther.url === 'xiazai') {
      params.url = 'xiazai'
      console.log(theOther)
    }
    // axios
    // .post(
    //   'http://web.zhiyunzaixian.com/wechat/Authorize/get_url_s',
    //   qs.stringify(params)
    // )
    getUrlS(qs.stringify(params)).then(res => {
      let { code, data } = res.data
      if (code === 200) {
        store.commit('member_id', data.member_id)
        store.commit('openid', data.encode_member_id)
        if (theOther && theOther.shareid) {
          Cookies.set('theRequest', '')
          Cookies.remove('theRequest', { path: '' })
          next({
            path: theOther.url,
            query: {
              cid: JSON.stringify(theOther)
            }
          })
        } else {
          next()
        }
      } else {
        login()
      }
    })
  } else {
    if (store.state.member_id === undefined || store.state.member_id === '') {
      // && to.name !== 'home'
      // 未登录且要跳转的页面不是登录页
      if (to.path === '/xiazai') {
        next()
        return false
      }
      login()
      // next({
      //   name: 'home' // 跳转到登录页
      // })
    } else if (store.state.member_id === undefined && to.name === 'home') {
      login()
      // 未登陆且要跳转的页面是登录页
      // next() // 跳转
    } else if (store.state.member_id !== undefined) {
      // http://education.ztwlxx.net/app_id=Zmli
      if (!to.name) {
        next({
          name: 'home'
        })
      } else {
        if (theOther && theOther.shareid) {
          Cookies.set('theRequest', '')
          next({
            path: theOther.url,
            query: {
              cid: JSON.stringify(theOther)
            }
          })
        } else {
          next()
        }
      }
    }
  }
})

// 获取权限
router.afterEach((to, from) => {
  let obj = {
    app_id: store.state.app_id,
    url: ''
  }
  if (window.__wxjs_is_wkwebview !== true) {
    // 安卓
    obj.url = window.location.origin + to.fullPath
  } else {
    // ios
    obj.url = window.entryUrl
    // alert('ios')
    // alert(obj.url)
  }
  obj.url = encodeURIComponent(obj.url)
  // axios
  //   .post('http://web.zhiyunzaixian.com/wechat/Authorize/wx_jssdk', qs.stringify(obj))
  jssdk(qs.stringify(obj)).then(res => {
    if (res.data.code === 200) {
      let _lists = res.data.data
      wx.config({
        debug: false,
        signType: 'MD5',
        appId: _lists.appId,
        timestamp: _lists.timestamp,
        nonceStr: _lists.nonceStr,
        signature: _lists.signature,
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
      })
    }
  })
})

// 第一次进入程序的url

// let obj = {
//   app_id: store.state.app_id,
//   url: ''
// }
// obj.url = window.entryUrl
// obj.url = encodeURIComponent(obj.url)
// axios
//   .post('http://web.zhiyunzaixian.com/wechat/Authorize/wx_jssdk', qs.stringify(obj))
//   .then(res => {
//     if (res.data.code === 200) {
//       let _lists = res.data.data
//       wx.config({
//         debug: true,
//         signType: 'MD5',
//         appId: _lists.appId,
//         timestamp: _lists.timestamp,
//         nonceStr: _lists.nonceStr,
//         signature: _lists.signature,
//         jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
//       })
//     }
//   })
